﻿using Microsoft.AspNetCore.Mvc;
using RuYiAdmin.Net.Entity.BusinessEntity.SystemManagement;
using RuYiAdmin.Net.Entity.BusinessEnum;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Service.BusinessService.SystemManagement.ScheduleJob;
using RuYiAdmin.Net.WebApi.AppCode.ActionFilters;
using RuYiAdmin.Net.WebApi.AppCode.FrameworkBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RuYiAdmin.Net.WebApi.Controllers.SystemManagement
{
    /// <summary>
    /// 计划任务控制器
    /// </summary>
    public class ScheduleJobManagementController : BaseController<SysScheduleJob>
    {
        /// <summary>
        /// 计划任务接口实例
        /// </summary>
        private readonly IScheduleJobService scheduleJobService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="scheduleJobService"></param>
        public ScheduleJobManagementController(IScheduleJobService scheduleJobService) : base(scheduleJobService)
        {
            this.scheduleJobService = scheduleJobService;
        }

        /// <summary>
        /// 查询计划任务列表
        /// </summary>
        /// <param name="queryCondition">查询条件</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        [Permission("job:query:list")]
        public IActionResult Post(QueryCondition queryCondition)
        {
            var actionResult = this.scheduleJobService.SqlQuery(queryCondition, "sqls:sql:query_sysschedulejob");
            return Ok(actionResult);
        }

        /// <summary>
        /// 查询计划任务
        /// </summary>
        /// <param name="jobId">任务编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{jobId}")]
        [Log(OperationType.QueryEntity)]
        [Permission("job:query:list")]
        public IActionResult GetById(Guid jobId)
        {
            var actionResult = this.scheduleJobService.GetById(jobId);
            return Ok(actionResult);
        }

        /// <summary>
        /// 新增计划任务
        /// </summary>
        /// <param name="scheduleJob">计划任务对象</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.AddEntity)]
        [Permission("job:add:entity")]
        public IActionResult Add([FromBody] SysScheduleJob scheduleJob)
        {
            scheduleJob.JobStatus = JobStatus.Planning;
            var actionResult = this.scheduleJobService.Add(scheduleJob);
            return Ok(actionResult);
        }

        /// <summary>
        /// 编辑计划任务
        /// </summary>
        /// <param name="scheduleJob">计划任务对象</param>
        /// <returns>ActionResult</returns>
        [HttpPut]
        [Log(OperationType.EditEntity)]
        [Permission("job:edit:entity")]
        public IActionResult Put([FromBody] SysScheduleJob scheduleJob)
        {
            var actionResult = this.scheduleJobService.Update(scheduleJob);
            return Ok(actionResult);
        }

        /// <summary>
        /// 删除计划任务
        /// </summary>
        /// <param name="jobId">任务编号</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{jobId}")]
        [Permission("job:del:entity")]
        [Log(OperationType.DeleteEntity)]
        public async Task<IActionResult> DeleteAsync(Guid jobId)
        {
            await this.scheduleJobService.DeleteScheduleJobAsync(jobId);
            var actionResult = this.scheduleJobService.Delete(jobId);
            return Ok(actionResult);
        }

        /// <summary>
        /// 添加计划任务
        /// </summary>
        /// <param name="jobId">任务编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{jobId}")]
        [Log(OperationType.AddScheduleJob)]
        [Permission("schedule:job:add")]
        public async Task<IActionResult> AddScheduleJobAsync(Guid jobId)
        {
            await this.scheduleJobService.AddScheduleJobAsync(jobId);
            return Ok("ok");
        }

        /// <summary>
        /// 暂停计划任务
        /// </summary>
        /// <param name="jobId">任务编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{jobId}")]
        [Log(OperationType.PauseScheduleJob)]
        [Permission("schedule:job:pause")]
        public async Task<IActionResult> PauseScheduleJob(Guid jobId)
        {
            await this.scheduleJobService.PauseScheduleJobAsync(jobId);
            return Ok("ok");
        }

        /// <summary>
        /// 启动计划任务
        /// </summary>
        /// <param name="jobId">任务编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{jobId}")]
        [Log(OperationType.ResumeScheduleJob)]
        [Permission("schedule:job:resume")]
        public async Task<IActionResult> ResumeScheduleJob(Guid jobId)
        {
            await this.scheduleJobService.ResumeScheduleJobAsync(jobId);
            return Ok("ok");
        }
    }
}
